package com.house365.core.task;

import android.content.Context;

import com.house365.core.R;
import com.house365.core.helper.VersionUpgradeHelper;
import com.house365.core.util.ActivityUtil;
import com.house365.core.util.DeviceUtil;

public class UpgradeTask extends CommonAsyncTask<Integer>{
	private int NET_ERROR=0;
	private int NEED_UPDATE=1;
	private int NO_NEED_UPDATE=2;
	VersionUpgradeHelper verUpgrade;
	boolean isAuto;
	public UpgradeTask(Context context,boolean isAuto) {
		super(context);
		this.isAuto = isAuto; 
		if(!isAuto){
			setLoadingresid(R.string.version_infoing);
		}
		verUpgrade = new VersionUpgradeHelper(context);
	}

	@Override
	public void onAfterDoInBackgroup(Integer result) {
		if(verUpgrade.getNewVersion() != null){
			if(result == NET_ERROR && !isAuto){
				ActivityUtil.showToast(context, R.string.net_error);
			}else{
				if (result == NEED_UPDATE) {
					verUpgrade.showUpdateDialog();
				} else {
					if(!isAuto){
						verUpgrade.notNewVersionShow();
					}
				}
			}
		}else {
			// 超时可能
			ActivityUtil.showToast(context, R.string.net_error);
		}
	}

	@Override
	public Integer onDoInBackgroup() {
		if(!DeviceUtil.isNetConnect(context)){
			return NET_ERROR;
		}else{
			boolean flag = verUpgrade.needUpdate();
			return flag ?NEED_UPDATE:NO_NEED_UPDATE;
		}
	}
	@Override
	protected void onNetworkUnavailable() {
		if(!isAuto){
			super.onNetworkUnavailable();
		}	
	}
	@Override
	protected void onHttpRequestError() {
		if(!isAuto){
			ActivityUtil.showToast(context, R.string.net_error);
		}
	}
	@Override
	protected void onParseError() {
		// TODO Auto-generated method stub
		super.onParseError();
	}

}
